package com.bootdo.shop.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bootdo.hjh.domain.ErrorConstant;
import com.bootdo.hjh.ex.ThrowUtils;
import com.bootdo.shop.dao.GoodsMapper;
import com.bootdo.shop.dao.ShoppingCartMapper;
import com.bootdo.shop.domain.GoodsConstant;
import com.bootdo.shop.domain.GoodsLimit;
import com.bootdo.shop.domain.request.ShoppingCartReq;
import com.bootdo.shop.domain.response.GoodsRes;
import com.bootdo.shop.domain.response.ShoppingCartRes;
import com.bootdo.shop.service.GoodsService;
import com.bootdo.shop.service.ShopOrderService;
import com.bootdo.shop.service.ShoppingCartService;


@Service
public class ShoppingCartServiceImpl implements ShoppingCartService{

	@Autowired
	private ShoppingCartMapper shoppingCartMapper;
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private ShopOrderService orderService;
	
	@Autowired
	private GoodsMapper goodsMapper;
	
	@Override
	public ShoppingCartReq addShoppingCart(ShoppingCartReq shoppingCartReq) {
		if(shoppingCartReq.getSpecificationId() == null){//如果规格不存在 规格置为0
			shoppingCartReq.setSpecificationId(0);
		}
		if(shoppingCartReq.getGoodsId() == null || shoppingCartReq.getGoodsId() <= 0){
			ThrowUtils.throwException(ErrorConstant.PARAM_ERROR.getErrorCode(), "商品ID不能为空");
		}
		if(shoppingCartReq.getGoodsNum()== null){
			ThrowUtils.throwException(ErrorConstant.PARAM_ERROR.getErrorCode(), "商品数量不能为空");
		}
		if(shoppingCartReq.getUserId() == null || shoppingCartReq.getUserId() <= 0){
			ThrowUtils.throwException(ErrorConstant.PARAM_ERROR.getErrorCode(), "用户ID不能为空");
		}
		GoodsRes goods = goodsService.getGoodsDetailById(shoppingCartReq.getGoodsId());
		
		if(goods.getAmount().intValue() < shoppingCartReq.getGoodsNum().intValue()){
			ThrowUtils.throwException(ErrorConstant.OPERATION_ERROR.getErrorCode(), "库存不足");
		}
		if(goods.getIsOnsale() == GoodsConstant.GOODS_ONSALE_STATUS_NOT.getStatus()){
			ThrowUtils.throwException(ErrorConstant.OPERATION_ERROR.getErrorCode(), "加入购物车失败,商品未上架");
		}
		if(goods.getIsOnsale() == GoodsConstant.GOODS_ONSALE_STATUS_UNDERCARRIAGE.getStatus()){
			ThrowUtils.throwException(ErrorConstant.OPERATION_ERROR.getErrorCode(), "加入购物车失败,商品已下架");
		}
		ShoppingCartReq scr = shoppingCartMapper.findShoppingCartByGoodsIdAndUserId(shoppingCartReq);
		//校验是否有限购
		orderService.verifyLimitations(shoppingCartReq.getUserId(), shoppingCartReq.getGoodsId(), shoppingCartReq.getSpecificationId(), shoppingCartReq.getGoodsNum());
		//如果存在更新购物车数量
		if(null != scr){
			shoppingCartMapper.updateNum(shoppingCartReq);
			Integer num = scr.getGoodsNum() + shoppingCartReq.getGoodsNum();
			shoppingCartReq.setGoodsNum(num <= 0 ? 1: num);
		}else{
			//不存在新增
			shoppingCartMapper.insert(shoppingCartReq);
		}
		return shoppingCartReq;
	}

	@Override
	public void deleteShoppingCart(ShoppingCartReq shoppingCartReq) {
		
		if(shoppingCartReq.getUserId() == null || shoppingCartReq.getUserId() <= 0){
			ThrowUtils.throwException(ErrorConstant.PARAM_ERROR.getErrorCode(), "用户ID不能为空");
		}
		if(shoppingCartReq.getSpecificationId() == null){//如果规格不存在 规格置为0
			shoppingCartReq.setSpecificationId(0);
		}
		ShoppingCartReq scr = shoppingCartMapper.findShoppingCartByGoodsIdAndUserId(shoppingCartReq);
		if(null != scr && scr.getUserId().intValue() != shoppingCartReq.getUserId().intValue()){
			if(shoppingCartReq.getUserId() == null || shoppingCartReq.getUserId() <= 0){
				ThrowUtils.throwException(ErrorConstant.PARAM_ERROR.getErrorCode(), "该购物车商品不属于您,不能删除");
			}
		}
		shoppingCartMapper.delete(scr.getId());
	}

	@Override
	public void updateShoppingCart(Long userId,ShoppingCartReq[] shoppingCartReq) {
		if(null == shoppingCartReq || shoppingCartReq.length == 0){
			return ;
		}
		if(userId == null || userId <= 0){
			ThrowUtils.throwException(ErrorConstant.PARAM_ERROR.getErrorCode(), "用户ID不能为空");
		}
		for(ShoppingCartReq scr : shoppingCartReq){
			if(null != scr && null != scr.getGoodsId()){
				if(scr.getSpecificationId() == null){//如果规格不存在 规格置为0
					scr.setSpecificationId(0);
				}
				scr.setUserId(userId);
				ShoppingCartReq shoppingCR = shoppingCartMapper.findShoppingCartByGoodsIdAndUserId(scr);
				if(null != shoppingCR) {
					//校验是否有限购
					orderService.verifyLimitations(shoppingCR.getUserId(), shoppingCR.getGoodsId(), 
							shoppingCR.getSpecificationId(), shoppingCR.getGoodsNum()  );
					shoppingCartMapper.update(scr);
				}
				
			}
		}
	}
	
	@Override
	public Integer getShoppingCartNum(ShoppingCartReq shoppingCartReq) {
		if(shoppingCartReq.getUserId() == null){
			ThrowUtils.throwException(ErrorConstant.PARAM_ERROR.getErrorCode(), "用户ID不能为空");
		}
		if(shoppingCartReq.getSpecificationId() == null){//如果规格不存在 规格置为0
			shoppingCartReq.setSpecificationId(0);
		}
		Integer num = shoppingCartMapper.findShoppingCartNum(shoppingCartReq);
		return num == null ? 0 : num;
	}

	@Override
	public List<ShoppingCartRes> getShoppingCartList(ShoppingCartReq shoppingCartReq) {
		if(shoppingCartReq.getUserId() == null){
			ThrowUtils.throwException(ErrorConstant.PARAM_ERROR.getErrorCode(), "用户ID不能为空");
		}
		if(shoppingCartReq.getSpecificationId() == null){//如果规格不存在 规格置为0
			shoppingCartReq.setSpecificationId(0);
		}
		List<ShoppingCartRes> ShoppingCartList = shoppingCartMapper.findShoppingCartList(shoppingCartReq);
		goodsService.coverPriceFprCart(shoppingCartReq.getMemberLevel(), shoppingCartReq.getAgentLevel(), ShoppingCartList);
		for(ShoppingCartRes scr : ShoppingCartList){
			//添加限购
			GoodsLimit goodsLimit = new GoodsLimit();
			goodsLimit.setGoodsId(scr.getGoodsId());
			List<GoodsLimit> goodsLimits = goodsMapper.getGoodsLimitList(goodsLimit);
			scr.setGoodsLimits(goodsLimits);
		}
		for(ShoppingCartRes scr : ShoppingCartList){
			if(scr.getIsDisable() == null){
				if(scr.getIsOnsale() == 1) {
					scr.setIsDisable(false);
				}
				else if(scr.getIsOnsale() == 0 || scr.getIsOnsale() == 2){
					//商品未上架或已下架，不可购买
					scr.setIsDisable(true);
				}
			}
		}
		return ShoppingCartList;
	}

}
